package xhwl.insurance.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lidroid.xutils.util.LogUtils;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;
import xhwl.android.common.util.SPUtil;
import xhwl.android.common.util.StringUtil;
import xhwl.android.common.util.ToastUtil;
import xhwl.insurance.BaseActivity;
import xhwl.insurance.R;
import xhwl.insurance.adapter.CustomerServiceAdapter;
import xhwl.insurance.dialog.CallPhoneDialog;
import xhwl.insurance.event.LayoutChangeEvent;
import xhwl.insurance.http.api.UserGuideApi;
import xhwl.insurance.http.bean.resp.UserGuideListResp;
import xhwl.pulltorefresh.PullToRefreshRecyclerView;
import xhwl.retrofitrx.HttpException;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;

public class CustomerServiceActivity extends BaseActivity implements View.OnClickListener{
    private PullToRefreshRecyclerView recyclerView;
    private RadioButton rb_smart_customer_service,rb_phone_customer_service,rb_manual_customer_service,rb_feedback;
    CustomerServiceAdapter adapter;
    ArrayList<UserGuideListResp> mList = new ArrayList<>();
    private String back_state;
    private ImageView title_left_imageview;
    private RadioGroup radioGroup;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setContentView(R.layout.activity_customerservice);
        back_state = getIntent().getStringExtra("BACK_STATE");
    }

    @Override
    public void initViews() {
        initTitleViews();
        rb_smart_customer_service = (RadioButton)findViewById(R.id.rb_smart_customer_service);
        rb_phone_customer_service = (RadioButton)findViewById(R.id.rb_phone_customer_service);
        rb_manual_customer_service = (RadioButton)findViewById(R.id.rb_manual_customer_service);
        rb_feedback = (RadioButton)findViewById(R.id.rb_feedback);
        title_left_imageview = (ImageView) findViewById(R.id.title_left_imageview);
        radioGroup = (RadioGroup)findViewById(R.id.main_radio1);
//        radioGroup.check(R.id.rb_smart_customer_service);// 默认选择；

        recyclerView = (PullToRefreshRecyclerView) findViewById(R.id.recyclerView);
        initRecylerView();
        adapter = new CustomerServiceAdapter(mContext,mList);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickLitener(new CustomerServiceAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(mContext, UserGudieDetailActivity.class);
                intent.putExtra("title", mList.get(position).getTitle());
                intent.putExtra("url", mList.get(position).getUrl());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        rb_smart_customer_service.setChecked(false);
        rb_phone_customer_service.setChecked(false);
        rb_manual_customer_service.setChecked(false);
        rb_feedback.setChecked(false);
    }
    public void onEvent(LayoutChangeEvent event) {
        rb_phone_customer_service.setChecked(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    private void initRecylerView() {

        //设置EmptyView
        View emptyView = View.inflate(mContext, R.layout.layout_empty_view, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        recyclerView.setEmptyView(emptyView);

        final LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        //设置是否开启上拉加载
        recyclerView.setLoadingMoreEnabled(false);
        //设置是否开启下拉刷新
        recyclerView.setPullRefreshEnabled(false);
        //设置是否显示上次刷新的时间
        recyclerView.displayLastRefreshTime(false);
//        recyclerView.setRefreshLimitHeight(DimenUtil.dip2px(mActivity,50));
        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
    }
    @Override
    protected void setUpTitleView() {
        titleTextView.setText("客户服务");
    }

    @Override
    public void initListener() {
        rb_smart_customer_service.setOnClickListener(this);
        rb_phone_customer_service.setOnClickListener(this);
        rb_manual_customer_service.setOnClickListener(this);
        rb_feedback.setOnClickListener(this);
        title_left_imageview.setOnClickListener(this);

    }


    @Override
    public void loadData() {
        getLoad(1);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rb_smart_customer_service:
                startActivity(new Intent(CustomerServiceActivity.this, SmartServiceActivity.class));
                break;
            case R.id.rb_phone_customer_service:
                CallPhoneDialog pd = new CallPhoneDialog(CustomerServiceActivity.this);
                pd.setCancelable(false);
                pd.setCanceledOnTouchOutside(false);
                pd.show();
                break;
            case R.id.rb_manual_customer_service:
                startActivity(new Intent(CustomerServiceActivity.this,StaffServiceActivity.class)
                        .putExtra("BACK_STATE","0"));
                break;
            case R.id.rb_feedback:
                startActivity(new Intent(CustomerServiceActivity.this, FeedBackActivity.class));
                break;
            case R.id.title_left_imageview://
                if(back_state.equals("1")) {
                    startActivity(new Intent(CustomerServiceActivity.this, MainActivity.class)
                            .putExtra("BACK_STATE","1"));
                    finish();
                }else {
                    finish();
                }
                break;
        }

    }
    public void getLoad(int page ) {
        String member_id = SPUtil.getPersonInfoSp().getString(SPUtil.SPPersonInfo.MEMBER_ID,null);
        if (StringUtil.isEmpty(member_id)) {
            return;
        }
        UserGuideApi api = new UserGuideApi(member_id,page);
        HttpManager.getInstance().dealHttp(this,api,new OnNextListener<ArrayList<UserGuideListResp>>(){
            @Override
            public void onNext(ArrayList<UserGuideListResp> resp) {
                mList.clear();
                mList.addAll(resp);
                adapter.notifyDataSetChanged();

            }

            @Override
            public void onError(HttpException ex) {
                super.onError(ex);

            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if(back_state.equals("1")) {
                startActivity(new Intent(CustomerServiceActivity.this, MainActivity.class)
                        .putExtra("BACK_STATE","1"));
                finish();
            }else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
